/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.das;

import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.portalcenter.common.enums.MenuTypeEnum;
import cn.hallele.mc.sdk.portalcenter.dto.MenuDTO;
import cn.hallele.mc.sdk.portalcenter.dto.MenuQueryDTO;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.portalcenter.application.bo.MenuBO;
import cn.hallele.ms.portalcenter.application.bo.MenuQueryBO;
import cn.hallele.ms.starter.web.framework.ActionExecutor;
import cn.hallele.ms.starter.web.framework.context.ActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/10/12 22:34
 */
@Slf4j
@Service
public class MenuDBDas extends ModelDas<ActionExecutor, MenuDTO, MenuBO> {
    protected MenuDBDas(ActionExecutor option) {
        super(option);
    }

    private static final String QUERY_ROUTE_LIST = "queryRouteListApplicationService";
    private static final String QUERY_MENU_LIST = "queryMenuListApplicationService";

    /**
     * 获取 路由列表
     *
     * @return List
     */
    public List<MenuDTO> getRoutes() {
        ActionContext context = new ActionContext();
        this.option.execute2Resp(QUERY_ROUTE_LIST, null, context);
        List<MenuBO> result = context.getResult();
        return revertList(result);
    }

    /**
     * 获取 菜单列表
     *
     * @param condition 查询条件
     * @return 系统菜单
     */
    public List<MenuDTO> getMenus(MenuQueryDTO condition) {
        ActionContext context = new ActionContext();
        this.option.execute2Resp(QUERY_MENU_LIST, convertQuery(condition), context);
        List<MenuBO> result = context.getResult();
        return revertList(result);
    }

    /**
     * 将T转成E
     *
     * @param routeDTO 被转换者
     * @return S 转换后者
     */
    @Override
    protected MenuBO convert(MenuDTO routeDTO) {
        return ObjectUtil.map(routeDTO, u -> MenuBO.builder()
                .id(u.getId())
                .parentId(u.getParentId())
                .title(u.getTitle())
                .name(u.getName())
                .type(MenuTypeEnum.valueOf(u.getMenuType()))
                .path(u.getPath())
                .component(u.getComponent())
                .perm(u.getPerm())
                .hide(u.getIsHide())
                .sort(u.getSort())
                .icon(u.getIcon())
                .redirect(u.getRedirect())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .alwaysShow(u.getAlwaysShow())
                .keepAlive(u.getIsKeepAlive())
                .full(u.getIsFull())
                .affix(u.getIsAffix())
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param routeBO 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected MenuDTO revert(MenuBO routeBO) {
        return ObjectUtil.map(routeBO, u -> MenuDTO.builder()
                .id(u.getId())
                .parentId(u.getParentId())
                .title(u.getTitle())
                .name(u.getName())
                .menuType(ObjectUtil.map(u.getType(), MenuTypeEnum::getCode))
                .path(u.getPath())
                .component(u.getComponent())
                .perm(u.getPerm())
                .isHide(u.getHide())
                .sort(u.getSort())
                .icon(u.getIcon())
                .redirect(u.getRedirect())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .alwaysShow(u.getAlwaysShow())
                .isKeepAlive(u.getKeepAlive())
                .isFull(u.getFull())
                .isAffix(u.getAffix())
                .isLink(ObjectUtil.equals(MenuTypeEnum.IFRAME, u.getType()) ? u.getPath() : "")
                .build());
    }

    /**
     * MenuQueryDTO 转 MenuQueryBO
     *
     * @param condition MenuQueryDTO
     * @return MenuQueryBO
     */
    private MenuQueryBO convertQuery(MenuQueryDTO condition) {
        return ObjectUtil.map(condition, d -> MenuQueryBO.builder()
                .keywords(d.getKeywords())
                .startTime(d.getStartTime())
                .endTime(d.getEndTime())
                .pageNo(d.getPageNo())
                .pageSize(d.getPageSize())
                .title(d.getTitle())
                .perm(d.getPerm())
                .status(d.getStatus())
                .build());
    }
}
